<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>员工管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 100px;
            font-weight: bold;
        }
        input[type="text"], input[type="email"], input[type="number"], select {
            width: 200px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        .search-section {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .pagination {
            margin-top: 20px;
            text-align: center;
        }
        .pagination button {
            margin: 0 5px;
        }
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>员工管理</h1>
        
        <div id="form-container">
            <h3 id="form-title">新增员工</h3>
            <form id="employee-form">
                <input type="hidden" id="employee-id">
                <div class="form-group">
                    <label for="username">用户名:</label>
                    <input type="text" id="username" required>
                </div>
                <div class="form-group">
                    <label for="name">姓名:</label>
                    <input type="text" id="name" required>
                </div>
                <div class="form-group">
                    <label for="password">密码:</label>
                    <input type="text" id="password" required>
                </div>
                <div class="form-group">
                    <label for="email">邮箱:</label>
                    <input type="email" id="email" required>
                </div>
                <div class="form-group">
                    <label for="age">年龄:</label>
                    <input type="number" id="age" required>
                </div>
                <div class="form-group">
                    <label for="admin">管理员:</label>
                    <select id="admin">
                        <option value="false">否</option>
                        <option value="true">是</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="deptId">部门:</label>
                    <select id="deptId" required>
                        <option value="">请选择部门</option>
                    </select>
                </div>
                <button type="submit" id="submit-btn">保存</button>
                <button type="button" onclick="resetForm()">重置</button>
            </form>
        </div>
        
        <div class="search-section">
            <h3>搜索条件</h3>
            <div class="form-group">
                <label for="search-name">姓名:</label>
                <input type="text" id="search-name" placeholder="支持模糊查询">
            </div>
            <div class="form-group">
                <label for="search-email">邮箱:</label>
                <input type="email" id="search-email" placeholder="支持模糊查询">
            </div>
            <div class="form-group">
                <label for="search-dept">部门:</label>
                <select id="search-dept">
                    <option value="">全部部门</option>
                </select>
            </div>
            <button onclick="searchEmployees()">搜索</button>
            <button onclick="loadEmployees()">重置搜索</button>
        </div>
        
        <div style="margin-top: 30px;">
            <button onclick="loadEmployees()">刷新列表</button>
        </div>
        
        <table id="employee-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>姓名</th>
                    <th>邮箱</th>
                    <th>年龄</th>
                    <th>管理员</th>
                    <th>部门</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="employee-tbody">
            </tbody>
        </table>
        
        <div class="pagination">
            <button onclick="changePage(-1)">上一页</button>
            <span id="page-info">第 1 页</span>
            <button onclick="changePage(1)">下一页</button>
        </div>
    </div>

    <script>
        let currentPage = 1;
        let totalPages = 1;
        let departments = [];
        
        // 页面加载时获取数据
        window.onload = function() {
            loadDepartments();
            loadEmployees();
        };
        
        // 加载部门列表
        function loadDepartments() {
            fetch('http://127.0.0.1:8080/api/departments')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        departments = data.data;
                        updateDepartmentSelects();
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }
        
        // 更新部门选择框
        function updateDepartmentSelects() {
            const deptSelect = document.getElementById('deptId');
            const searchDeptSelect = document.getElementById('search-dept');
            
            // 清空现有选项
            deptSelect.innerHTML = '<option value="">请选择部门</option>';
            searchDeptSelect.innerHTML = '<option value="">全部部门</option>';
            
            // 添加部门选项
            departments.forEach(dept => {
                deptSelect.innerHTML += `<option value="${dept.id}">${dept.name}</option>`;
                searchDeptSelect.innerHTML += `<option value="${dept.id}">${dept.name}</option>`;
            });
        }
        
        // 加载员工列表
        function loadEmployees() {
            fetch(`http://127.0.0.1:8080/api/employees/page?page=${currentPage}&size=10`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayEmployees(data.data);
                        totalPages = Math.ceil(data.total / 10);
                        updatePageInfo();
                    } else {
                        alert('加载失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('加载失败');
                });
        }
        
        // 搜索员工
        function searchEmployees() {
            const name = document.getElementById('search-name').value;
            const email = document.getElementById('search-email').value;
            const deptId = document.getElementById('search-dept').value;
            
            let url = `http://127.0.0.1:8080/api/employees/search?page=${currentPage}&size=10`;
            if (name) url += `&name=${encodeURIComponent(name)}`;
            if (email) url += `&email=${encodeURIComponent(email)}`;
            if (deptId) url += `&deptId=${deptId}`;
            
            fetch(url)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayEmployees(data.data);
                        totalPages = Math.ceil(data.total / 10);
                        updatePageInfo();
                    } else {
                        alert('搜索失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('搜索失败');
                });
        }
        
        // 显示员工列表
        function displayEmployees(employees) {
            const tbody = document.getElementById('employee-tbody');
            tbody.innerHTML = '';
            
            employees.forEach(emp => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${emp.id}</td>
                    <td>${emp.username}</td>
                    <td>${emp.name}</td>
                    <td>${emp.email}</td>
                    <td>${emp.age}</td>
                    <td>${emp.admin ? '是' : '否'}</td>
                    <td>${emp.department ? emp.department.name : ''}</td>
                    <td>
                        <button onclick="editEmployee(${emp.id})">编辑</button>
                        <button class="btn-danger" onclick="deleteEmployee(${emp.id})">删除</button>
                    </td>
                `;
                tbody.appendChild(row);
            });
        }
        
        // 编辑员工
        function editEmployee(id) {
            fetch(`http://127.0.0.1:8080/api/employees/${id}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        const emp = data.data;
                        document.getElementById('employee-id').value = emp.id;
                        document.getElementById('username').value = emp.username;
                        document.getElementById('name').value = emp.name;
                        document.getElementById('password').value = emp.password;
                        document.getElementById('email').value = emp.email;
                        document.getElementById('age').value = emp.age;
                        document.getElementById('admin').value = emp.admin.toString();
                        document.getElementById('deptId').value = emp.deptId || '';
                        document.getElementById('form-title').textContent = '编辑员工';
                        document.getElementById('submit-btn').textContent = '更新';
                    } else {
                        alert('获取员工信息失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('获取员工信息失败');
                });
        }
        
        // 删除员工
        function deleteEmployee(id) {
            if (confirm('确定要删除这个员工吗？')) {
                fetch(`http://127.0.0.1:8080/api/employees/${id}`, {
                    method: 'DELETE'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        loadEmployees();
                    } else {
                        alert('删除失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败');
                });
            }
        }
        
        // 表单提交
        document.getElementById('employee-form').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const id = document.getElementById('employee-id').value;
            const username = document.getElementById('username').value;
            const name = document.getElementById('name').value;
            const password = document.getElementById('password').value;
            const email = document.getElementById('email').value;
            const age = document.getElementById('age').value;
            const admin = document.getElementById('admin').value === 'true';
            const deptId = document.getElementById('deptId').value;
            
            const employee = {
                username: username,
                name: name,
                password: password,
                email: email,
                age: parseInt(age),
                admin: admin,
                deptId: deptId ? parseInt(deptId) : null
            };
            
            const url = id ? `http://127.0.0.1:8080/api/employees/${id}` : 'http://127.0.0.1:8080/api/employees';
            const method = id ? 'PUT' : 'POST';
            
            fetch(url, {
                method: method,
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(employee)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(id ? '更新成功' : '新增成功');
                    resetForm();
                    loadEmployees();
                } else {
                    alert((id ? '更新' : '新增') + '失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert((id ? '更新' : '新增') + '失败');
            });
        });
        
        // 重置表单
        function resetForm() {
            document.getElementById('employee-form').reset();
            document.getElementById('employee-id').value = '';
            document.getElementById('form-title').textContent = '新增员工';
            document.getElementById('submit-btn').textContent = '保存';
        }
        
        // 切换页面
        function changePage(delta) {
            const newPage = currentPage + delta;
            if (newPage >= 1 && newPage <= totalPages) {
                currentPage = newPage;
                loadEmployees();
            }
        }
        
        // 更新页面信息
        function updatePageInfo() {
            document.getElementById('page-info').textContent = `第 ${currentPage} 页，共 ${totalPages} 页`;
        }
    </script>
</body>
</html> 